package com.wn.date20190820Socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 处理socket请求
 * 
 * @author song
 *
 */
public class SocketThread implements Runnable
{
	private Socket socket;

	public SocketThread(Socket socket)
	{
		this.socket = socket;
	}

	@Override
	public void run()
	{
		try (InputStream is = socket.getInputStream();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		)
		{
			String line = null;
			// 输出
			while ((line = reader.readLine()) != null)
			{
				System.out.println(socket.getRemoteSocketAddress()+"："+line);
			}
			// 关闭
			socket.shutdownInput();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		// 关闭
		finally {
			if(socket!=null)
				try
				{
					socket.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
		}

	}

}
